package cc.customer.system.service.impl;

import cc.customer.cRound.domain.CRoundConfig;
import cc.customer.common.domain.ResponseBo;
import cc.customer.common.service.impl.BaseService;
import cc.customer.system.dao.CDingConfigMapper;
import cc.customer.system.dao.CDingUserMapper;
import cc.customer.system.domain.CDingConfig;
import cc.customer.system.domain.CDingUser;
import cc.customer.system.service.DingConfigServices;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Description: customer
 * Created by yhsh on 2019/12/6 10:24
 * version 2.0
 * 方法说明
 */
@Service
public class DingConfigServicesImpl extends BaseService<CDingConfig>  implements DingConfigServices {

	private static final Logger logger = LoggerFactory.getLogger(DingConfigServicesImpl.class);

	@Autowired
	private CDingConfigMapper cDingConfigMapper;

	@Autowired
	private CDingUserMapper cDingUserMapper;

	@Override
	public ResponseBo addOrUpdateConfg(CDingConfig cdingConfig) {
		try {
			Example example = new Example(CDingConfig.class);
			example.createCriteria().andEqualTo("cExName",cdingConfig.getcExName()).andEqualTo("cExType",cdingConfig.getcExType());
			List<CDingConfig> list = this.selectByExample(example);
			if (list.size() >0){
				return ResponseBo.error("当前项目已经存在");
			}
			return ResponseBo.ok();
		}catch (Exception e){
			logger.error("新建失败");
		}
		return ResponseBo.error("新建失败");
	}

	@Override
	public ResponseBo selectConfig(CDingConfig config) {
		try {
			List<CDingConfig> list =cDingConfigMapper.getAllInter(config.getcExType());
			return ResponseBo.ok(list);
		}catch (Exception e){
			e.printStackTrace();
			logger.error("查询失败");
		}
		 return ResponseBo.error();
	}

	@Override
	public ResponseBo deleteSelectInter(String exName,String type) {
		try {
			Example example = new Example(CDingConfig.class);
			example.createCriteria().andEqualTo("cExName",exName).andEqualTo("cExType",type);
			int num  = cDingConfigMapper.deleteByExample(example);
			return num >0 ?ResponseBo.ok():ResponseBo.error();
		}catch (Exception e){
			logger.error("删除失败"+e.getMessage());
		}
		return ResponseBo.error();
	}

	@Override
	public ResponseBo selectDingUserByDeptid(String exName,String deptid,String type) {
		try {
			Example exampleDept = new Example(CDingConfig.class);
			exampleDept.createCriteria().andEqualTo("cExName",exName).andEqualTo("cExDept",deptid).andEqualTo("cExType",type);
			List<CDingConfig> listDept = this.selectByExample(exampleDept);
			List<String> list = new ArrayList<>();
			String authon = "0";
			String intern = "0";
			if (listDept.size() >0 ){
				CDingConfig cDingConfig = listDept.get(0);
				authon = cDingConfig.getcExAithon();
				intern = cDingConfig.getcExExname();
				String  users = cDingConfig.getcExUser();
				if (!StringUtils.isEmpty(users)){
					list.addAll(Arrays.asList(users.split(",")));
				}
			}
			Example example = new Example(CDingUser.class);
			example.createCriteria().andEqualTo("cDDept",deptid);
			List<CDingUser> listUsers =cDingUserMapper.selectByExample(example);
			JSONArray array = new JSONArray();

			JSONObject jsonObject = new JSONObject();
			jsonObject.put("selected",true);

			listUsers.forEach( lis ->{
				JSONObject json = new JSONObject();
				json.put("id",lis.getcDUsername());
				json.put("text",lis.getcDName());
				if (list.contains(lis.getcDUsername())){ json.put("state",jsonObject); }
				json.put("icon",null);
				array.add(json);
			});
			JSONObject jsonObject1 = new JSONObject();
			jsonObject1.put("dept",authon);
			jsonObject1.put("intern",intern);
			return ResponseBo.ok(jsonObject1,array);
		}catch (Exception e){
			logger.error("查询部门失败"+e.getMessage());
		}
		return ResponseBo.error();
	}

	@Override
	public ResponseBo saveDingUserConfig(CDingConfig cDingConfig) {
		try {
			Example exampleDept = new Example(CDingConfig.class);
			exampleDept.createCriteria().andEqualTo("cExName",cDingConfig.getcExName())
					.andEqualTo("cExDept",cDingConfig.getcExDept()).andEqualTo("cExType",cDingConfig.getcExType());
			List<CDingConfig> listDept = this.selectByExample(exampleDept);
			int num = 0;
			if (listDept.size() <=0){
				cDingConfig.setcExAithon("0");
				cDingConfig.setcExExname("0");
				num = this.save(cDingConfig);
			}else{
				num = cDingConfigMapper.updateIntern(cDingConfig);
			}
			return num >0?ResponseBo.ok():ResponseBo.error();
		}catch (Exception e){
			e.printStackTrace();
			logger.error("保存推送模板失败");
		}
		return ResponseBo.error();
	}

	@Override
	public CDingConfig selectAllCdingConfig(String exName, String deptid,String type) {
		try {
			Example exampleDept = new Example(CDingConfig.class);
			exampleDept.createCriteria().andEqualTo("cExName",exName).andEqualTo("cExDept",deptid).andEqualTo("cExType",type);
			List<CDingConfig> listDept = this.selectByExample(exampleDept);
			if (!listDept.isEmpty()){
				return listDept.get(0);
			}
		}catch (Exception e){
			logger.error("获取推送钉钉的配置部门失败");
		}
		return null;
	}


	@Override
	public ResponseBo selectDingByDept(String deptid,String type){
		try {
			List<CDingUser> listUsers =cDingUserMapper.selectDingUserByDept(deptid);
			JSONArray array = new JSONArray();

			JSONObject jsonObject = new JSONObject();
			jsonObject.put("selected",true);

			listUsers.forEach( lis ->{
				JSONObject json = new JSONObject();
				json.put("id",lis.getcDUsername());
				String grade = "";
				if(!StringUtils.isEmpty(lis.getcDGrade())){
					grade = lis.getcDGrade();
				}
				json.put("text",lis.getcDName()+"&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;"+grade);
//				if (list.contains(lis.getcDUsername())){ json.put("state",jsonObject); }
				json.put("icon",null);
				array.add(json);
			});
			return ResponseBo.ok("",array);
		}catch (Exception e){
			e.printStackTrace();
		}
		return ResponseBo.error();
	}


}
